//
//  CommodityHeaderView.swift
//  iPhone
//
//  Created by Toj on 8/9/22.
//

import UIKit

protocol CommodityHeaderViewDelegate: NSObjectProtocol {
    
    func commodityHeaderView(_ view: CommodityHeaderView, didTapSegment index: Int, title: String)
}

class CommodityHeaderView: UIView {
    
    var model: NSObject? {
        didSet {
            accountInfoView.model = nil
        }
    }
    
    weak var delegate: CommodityHeaderViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(accountInfoView)
        addSubview(segmentView)
        addSubview(freeIcon)
        
        setupMakeInitialize()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        accountInfoView.snp.makeConstraints { make in
            make.right.top.left.equalTo(self)
            make.height.equalTo(64)
        }
        
        segmentView.snp.makeConstraints { make in
            make.right.bottom.left.equalTo(self)
            make.height.equalTo(40)
        }
        
        let left = CGFloat.sr_width / 3 * 2 - 40 - 8
        freeIcon.snp.makeConstraints { make in
            make.top.equalTo(segmentView)
            make.width.equalTo(40)
            make.left.equalTo(left)
            make.height.equalTo(14)
        }
    }
    
    lazy var accountInfoView: STAccountInfoView = {
        let view = STAccountInfoView()
        view.isArrows = false
        view.backgroundColor = UIColor(hex: "#171B22")
         return view
    }()
    
    lazy var segmentView: STCommoditySegmentView = {
       let view = STCommoditySegmentView()
        view.delegate = self
        view.segmentBackgroundColor = UIColor(hex: "#28333B")
        return view
    }()
    
    lazy var freeIcon: UIImageView = {
       let view = UIImageView()
        view.image = "commodity_free_icon".to_image
        return view
    }()
    
    deinit {
        // print("-->toj: CommodityHeaderView: deinit")
    }
    
    private func setupMakeInitialize() {
        
    }
}

extension CommodityHeaderView: STCommoditySegmentViewDelegate {
    func commoditySegmentView(_ view: STCommoditySegmentView, didTapSegment index: Int, title: String) {
        delegate?.commodityHeaderView(self, didTapSegment: index, title: title)
    }
}
